﻿
//绑定页面控件
function bindControl(strJson, callback) {
    if (json == "") {
        return;
    }
    var json = strJson.toJson(); ;
    var notexiststable = json.notexiststables;
    $("#div_notexists").html("");
    if (notexiststable != "") {//检验数据表不存在
        $("<div id='div_notexists'></div>").html(notexiststable + "数据表在数据库中不存在，请检查!").css("color", "red").appendTo($("body"));
        var table = notexiststable.split(",");
        for (var t = 0; t < table.length; t++) {
            setNotExistsControlStyle(table[t]);
        }
    }
    for (var t = 0; t < json.rows.length; t++) {
        if (!json.rows[t]) {
            return;
        }
        var _tables = json.rows[t].table;
        var _tablenames = json.rows[t].tablename;
        var _tcount = _tables.length;
        //判断是否有多行显示
        var showmorefield = showMoreFieldRow(_tablenames);
        var showmorevalue = showMoreValueRow(_tablenames);
        if (showmorefield.length > 0) {
            for (var _m = 0; _m < showmorefield.length; _m++) {
                var _showmorefield = showmorefield[_m];
                var _showmorevalue = showmorevalue[_m]
                var _tcount = jsonFindCount(_tables, _showmorefield, _showmorevalue);
                var _sjsonold = _tables;
                var _sJson = jsonFindVal(_sjsonold, _showmorefield, _showmorevalue);
                cloneArea(_tablenames, _tcount, _showmorefield, _showmorevalue);
                bindData(_sJson, _tcount, _tablenames, _showmorefield, _showmorevalue);
            }
        }
        else {
            bindData(_tables, _tcount, _tablenames);
        }
        var right = $.query.get("right");
        if (right && right == "1") {

        }
        else {
            var obj_defult = $(document).find("[defultvalue]");
            for (var s = 0; s < obj_defult.length; s++) {
                var truetype = $(obj_defult[s]).attr("truetype");
                var defultvalue = $(obj_defult[s]).attr("defultvalue");

                if (truetype != "selectTree") {
                    if ($(obj_defult[s]).val() == "" || $(obj_defult[s]).val() == null) {
                        $(obj_defult[s]).val(obj_defult[s].defult);
                    }
                }
                else {
                    if ($(obj_defult[s]).attr("relValue") == "") {
                        $(obj_defult[s]).attr("hidValue", defultvalue);
                        $(obj_defult[s]).render();
                        $(obj_defult[s]).setValue(defultvalue);
                    }
                }
            }
        }
    }
    if (callback && callback != null && callback != "") {
        callback();
    }
}

//获取页面控件的id和对应的值
function GetPageCtrls(ctrlId,tabName,tabKeyName,tabKeyValue) {
    var objs = $("#" + ctrlId).find("input[name^='txt'],textarea[name^='txt'],input[name^='cbx'],select[name^='ddl']");
    var arrCtrlAndValues = [];
    for (var i = 0; i < objs.length; i++) {
        if ($(objs[i]).attr("id") != null) {//去掉重复项
            var strId = $(objs[i]).attr("id");
            var strFieldName = strId.replace("txt", "").replace("cbx", "").replace("ddl", "");
            var strValue = $(objs[i]).val();

            //一下checkbox的值获取特殊处理
            if (strId.substring(0, 3) == "cbx") {
                if (typeof ($(objs[i]).attr("checked")) == "undefined" || $(objs[i]).attr("checked") == "false") {
                    strValue = "0";
                }
                else {
                    strValue = "1";
                }
            }

            arrCtrlAndValues.push("'" + strFieldName + "':'" + strValue + "'");
        }
    }
    var strReturn = "{'tabname':'" + tabName + "','tabkeyname':'" + tabKeyName + "','tabkeyvalue':'" + tabKeyValue + "'";
    strReturn += ",'tabledata':[{" + arrCtrlAndValues.join(",") + "}]}";
    return strReturn;
}

//初始化控件的值
function LoadCtrlsValue(json) {
    if (json == "") {
        return;
    }

    json = json.toJson(); //格式化
    var strInner = "";
    for (var i = 0; i < json.length; i++) {
        o1 = json[i];
        for (key in o1) {
            var objs = $(document).find("input[name$='" + key + "'],textarea[name$='" + key + "'],select[name$='" + key + "']");

            if ($(objs).attr("id") != null) {
                $(objs).val(o1[key].toString());//默认是textbox

                if ($(objs).attr("id").substring(0, 3) == "ddl") {//下拉框
                    $(objs).find("option[value='" + o1[key].toString() + "']").attr("selected", true);
                    $(objs).selectmenu('refresh');
                }
                else if ($(objs).attr("id").substring(0, 3) == "cbx") {//选择框
                    if (o1[key].toString() == "1") {
                        $(objs).attr("checked", "true");
                        $(objs).checkboxradio('refresh');
                    }
                }

            }
        }
    }
}

//获取数据字典的值
function GetSjzdValues(strType,ctrlId,appearType) {
    var paramsInfo = "{'tabname':'ZS_PARAMS','tabkeyname':'name','tabkeyvalue':'" + strType + "'}";
    var nReturn = GetServerDataJson("Common.CommonPageOperation", "GetTableInfo", paramsInfo, "m", "");
    LoadDropDownListValue(nReturn, ctrlId, appearType);
}

//绑定下拉框的值
function LoadDropDownListValue(json, ctrlId, appearType) {
    if (json == "") {
        return;
    }
    json = json.toJson(); //格式化
    var strInner = "";

    var objs = $(document).find("select[name$='" + ctrlId + "']");
    for (var i = 0; i < json.length; i++) {
        o1 = json[i];

        if (appearType == 0 || appearType==null) {//值为数据字典的键值码
            strInner += "<option value=\"" + o1["keyvalue"].toString() + "\">" + o1["keyname"].toString() + "</option>";
        }
        else {//值为数据字典的中文名
            strInner += "<option value=\"" + o1["keyname"].toString() + "\">" + o1["keyname"].toString() + "</option>";
        }
    }

    $(objs).html(strInner);
}